{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1ad42d4f",
   "metadata": {},
   "source": [
    "* [文档调用网站](https://console.faceplusplus.com.cn/documents/4887579)\n",
    "* [apikey](https://console.faceplusplus.com.cn/app/apikey/list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "973a23ab",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T14:00:58.219792Z",
     "start_time": "2022-03-21T14:00:58.210792Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6894d9f0",
   "metadata": {},
   "source": [
    "### 1. 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "464f6cf9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T14:01:00.297911Z",
     "start_time": "2022-03-21T14:00:59.270852Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key = 'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_'\n",
    "API_sercret = 'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9'\n",
    "\n",
    "# 3.POST\n",
    "\n",
    "\n",
    "# 4.所有API key 都可以调用本API\n",
    "#    其中calculate_all 和 face_rectangle参数只有正式API Key 才能使用 ，试用API key 无法使用\n",
    "\n",
    "# 5.payload：酬载，必要阅读api文档中的*必选项*和*可选性*，及*参数说明*\n",
    "payload = {\n",
    "    'api_key':'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_',\n",
    "    'api_secret':'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9',\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "       \n",
    "}\n",
    "\n",
    "r = requests.post(base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "170121d7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T11:57:56.094559Z",
     "start_time": "2022-03-21T11:57:56.075558Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c00a939e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T11:57:59.426750Z",
     "start_time": "2022-03-21T11:57:59.415749Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647863873,f23b87a7-3255-42e0-aded-ea267e6c5773',\n",
       " 'time_used': 416,\n",
       " 'faces': [{'face_token': '13b646dfdc68464f90470d2dc5001075',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "075439cd",
   "metadata": {},
   "source": [
    "### 2. 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "99e7078f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T11:58:31.833603Z",
     "start_time": "2022-03-21T11:58:31.451581Z"
    }
   },
   "outputs": [],
   "source": [
    "# 2.人脸分析（请尝试调用analyze接口）\n",
    "\n",
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key = 'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_'\n",
    "API_sercret = 'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9'\n",
    "\n",
    "# 3.POST\n",
    "\n",
    "\n",
    "# 4.所有API key 都可以调用本API\n",
    "#    其中calculate_all 和 face_rectangle参数只有正式API Key 才能使用 ，试用API key 无法使用\n",
    "\n",
    "# 5.payload：酬载，必要阅读api文档中的*必选项*和*可选性*，及*参数说明*\n",
    "payload = {\n",
    "    'api_key':'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_',\n",
    "    'api_secret':'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9',\n",
    "    'face_tokens':'13b646dfdc68464f90470d2dc5001075',\n",
    "    'return_attributes':'gender,age,smiling,emotion,beauty'\n",
    "\n",
    "       \n",
    "}\n",
    "\n",
    "r = requests.post(base_url,params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e33c9be6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T11:58:34.206739Z",
     "start_time": "2022-03-21T11:58:34.197738Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fe190c22",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T11:58:36.923894Z",
     "start_time": "2022-03-21T11:58:36.895893Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 122,\n",
       " 'request_id': '1647863912,637ca805-7bc4-4a41-87a4-0644ffbe8cee',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.681,\n",
       "     'neutral': 0.007,\n",
       "     'disgust': 1.213,\n",
       "     'anger': 0.007,\n",
       "     'surprise': 0.009,\n",
       "     'fear': 0.112,\n",
       "     'happiness': 97.97},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'beauty': {'female_score': 56.631, 'male_score': 56.694},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '13b646dfdc68464f90470d2dc5001075'}]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "31ec0cb4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T11:58:53.494842Z",
     "start_time": "2022-03-21T11:58:43.690281Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "34052c31",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T11:58:53.863863Z",
     "start_time": "2022-03-21T11:58:53.526844Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>13b646dfdc68464f90470d2dc5001075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.681</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>0.007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>1.213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>97.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>56.631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>56.694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                               0\n",
       "face_token                      13b646dfdc68464f90470d2dc5001075\n",
       "attributes.emotion.sadness                                 0.681\n",
       "attributes.emotion.neutral                                 0.007\n",
       "attributes.emotion.disgust                                 1.213\n",
       "attributes.emotion.anger                                   0.007\n",
       "attributes.emotion.surprise                                0.009\n",
       "attributes.emotion.fear                                    0.112\n",
       "attributes.emotion.happiness                               97.97\n",
       "attributes.gender.value                                     Male\n",
       "attributes.age.value                                          53\n",
       "attributes.beauty.female_score                            56.631\n",
       "attributes.beauty.male_score                              56.694\n",
       "attributes.smile.threshold                                  50.0\n",
       "attributes.smile.value                                     100.0\n",
       "face_rectangle.width                                         107\n",
       "face_rectangle.top                                           108\n",
       "face_rectangle.left                                          141\n",
       "face_rectangle.height                                        107"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "70942221",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:00:10.708258Z",
     "start_time": "2022-03-21T12:00:09.704201Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key = 'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_'\n",
    "API_sercret = 'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9'\n",
    "\n",
    "# 3.POST\n",
    "\n",
    "\n",
    "# 4.所有API key 都可以调用本API\n",
    "#    其中calculate_all 和 face_rectangle参数只有正式API Key 才能使用 ，试用API key 无法使用\n",
    "\n",
    "# 5.payload：酬载，必要阅读api文档中的*必选项*和*可选性*，及*参数说明*\n",
    "payload = {\n",
    "    'api_key':'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_',\n",
    "    'api_secret':'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9',\n",
    "    'image_url':'https://tse4-mm.cn.bing.net/th/id/OIP-C.W477vr2p1_t7vgT0dGAnWgAAAA?w=182&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7'\n",
    "       \n",
    "}\n",
    "\n",
    "r = requests.post(base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "acfb9f68",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:00:13.016390Z",
     "start_time": "2022-03-21T12:00:13.009390Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647864010,cace5388-1fc7-429f-b52e-2a4919b43a5f',\n",
       " 'time_used': 779,\n",
       " 'faces': [{'face_token': '38e07afae8d6de2b07b9f73ba421a140',\n",
       "   'face_rectangle': {'top': 94, 'left': 32, 'width': 105, 'height': 105}}],\n",
       " 'image_id': 'vmOnYtPD4SVyllfsjrn1Mw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7a210325",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:00:37.467789Z",
     "start_time": "2022-03-21T12:00:37.460789Z"
    }
   },
   "outputs": [],
   "source": [
    "liu1 = '13b646dfdc68464f90470d2dc5001075'\n",
    "liu2 = '38e07afae8d6de2b07b9f73ba421a140'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be9e4859",
   "metadata": {},
   "source": [
    "### 3. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a5afb5c6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:03:04.848219Z",
     "start_time": "2022-03-21T12:03:04.375192Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key = 'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_'\n",
    "API_sercret = 'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9'\n",
    "\n",
    "# 3.POST\n",
    "\n",
    "\n",
    "# 4.所有API key 都可以调用本API\n",
    "#    其中calculate_all 和 face_rectangle参数只有正式API Key 才能使用 ，试用API key 无法使用\n",
    "\n",
    "# 5.payload：酬载，必要阅读api文档中的*必选项*和*可选性*，及*参数说明*\n",
    "payload = {\n",
    "    'api_key':'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_',\n",
    "    'api_secret':'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9',\n",
    "    'face_token1': '13b646dfdc68464f90470d2dc5001075',\n",
    "    'face_token2': '38e07afae8d6de2b07b9f73ba421a140'\n",
    "       \n",
    "}\n",
    "\n",
    "r = requests.post(base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0a4fa079",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:03:08.814446Z",
     "start_time": "2022-03-21T12:03:08.806445Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647864185,5481d421-a012-4db3-8b6d-07633b0e7c3f',\n",
       " 'time_used': 251,\n",
       " 'confidence': 86.705,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d86d237b",
   "metadata": {},
   "source": [
    "### 4. 人脸检测函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "416ab3cc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T14:03:58.983131Z",
     "start_time": "2022-03-21T14:03:58.972131Z"
    }
   },
   "outputs": [],
   "source": [
    "# 思考：能否将一些功能作函数封装，实现更加便捷的API调用\n",
    "def face_dect(API_key,API_sercret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max\n",
    "\n",
    "=100):\n",
    "    \"\"\"face++人脸检测接口函数（图片链接）\"\"\"\n",
    "    url='https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    paylooad={\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url': image_url,\n",
    "    'return_landmark':landmark,\n",
    "    'return_attributes':attributes,\n",
    "    'face_rectangle':face_rectangle,\n",
    "    'beauty_score_min':beauty_score_min,\n",
    "    'beauty_score_max':beauty_score_max\n",
    "    }\n",
    "    r= requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5f53d6b",
   "metadata": {},
   "source": [
    "#### 函数测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "611d3868",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T14:04:02.322322Z",
     "start_time": "2022-03-21T14:04:01.477274Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647871442,44ccc506-5eb1-4738-8e78-d8d4702f2b3d',\n",
       " 'time_used': 488,\n",
       " 'faces': [{'face_token': '7951ec0c9fc9c90dbc2337df8120e16b',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_dect(API_key,API_sercret,'https://tse4-mm.cn.bing.net/th/id/OIP-C.W477vr2p1_t7vgT0dGAnWgAAAA?w=182&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69200d69",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:11:35.587431Z",
     "start_time": "2022-03-21T12:11:35.580431Z"
    }
   },
   "source": [
    "### 5.人脸分析函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "7ae17b6b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T14:08:29.930629Z",
     "start_time": "2022-03-21T14:08:29.924628Z"
    }
   },
   "outputs": [],
   "source": [
    "def face_analyze(API_key,API_sercret,face_tokens,landmark=0,attributes='gender,age,smiling,headpose,facequality,blur,eyestatus,emotion',beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++人脸分析接口函数（图片token）\"\"\"\n",
    "    url2 = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload2 = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens': face_tokens,\n",
    "    'return_landmark':landmark,\n",
    "    'return_attributes':attributes,\n",
    "    'beauty_score_min':beauty_score_min,\n",
    "    'beauty_score_max':beauty_score_max    \n",
    "    }\n",
    "    r=requests.post(url2,payload2)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35487ab8",
   "metadata": {},
   "source": [
    "#### test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "cd659b95",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T14:08:35.813965Z",
     "start_time": "2022-03-21T14:08:35.392941Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 153,\n",
       " 'request_id': '1647871716,cd5309a4-580b-44d7-8edf-60dd304fb9de',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.681,\n",
       "     'neutral': 0.007,\n",
       "     'disgust': 1.213,\n",
       "     'anger': 0.007,\n",
       "     'surprise': 0.009,\n",
       "     'fear': 0.112,\n",
       "     'happiness': 97.97},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.002,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.998,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.031,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.008,\n",
       "      'no_glass_eye_open': 99.961,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'glass': {'value': 'None'},\n",
       "    'headpose': {'yaw_angle': 0.004189467,\n",
       "     'pitch_angle': 16.763775,\n",
       "     'roll_angle': 2.3050764},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.364},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.364},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.364}},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'facequality': {'threshold': 70.1, 'value': 85.658}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '7951ec0c9fc9c90dbc2337df8120e16b'}]}"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_analyze(API_key,API_sercret,'7951ec0c9fc9c90dbc2337df8120e16b')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5460c86f",
   "metadata": {},
   "source": [
    "### 6. 人脸比对函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "51a5e2bf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:57:16.890133Z",
     "start_time": "2022-03-21T13:57:16.840130Z"
    }
   },
   "outputs": [],
   "source": [
    "def face_compare(API_key,API_sercret,tokens1,tokens2,face_rectangle1=None,face_rectangle2=None):\n",
    "    \"\"\"face++人脸比对接口函数（图片token）\"\"\"\n",
    "    url3 = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload3 = {\n",
    "    'api_key':API_key,\n",
    "    'api_sercret':API_sercret,\n",
    "    'face_tokens1': face_tokens1,    \n",
    "    'face_tokens2': face_tokens2,\n",
    "    'face_rectangle1':face_rectangle1,\n",
    "    'face_rectangle2':face_rectangle2\n",
    "    }\n",
    "    r=requests.post(url3,payload3)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05503a00",
   "metadata": {},
   "source": [
    "#### test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88b00815",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_compare(API_key,API_sercret,"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d8fe490",
   "metadata": {},
   "source": [
    "### 7. 创建人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "10a633e0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:13:38.321359Z",
     "start_time": "2022-03-21T13:13:37.932337Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key = 'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_'\n",
    "API_sercret = 'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9'\n",
    "\n",
    "# 3.POST\n",
    "\n",
    "\n",
    "# 4.所有API key 都可以调用本API\n",
    "#    其中calculate_all 和 face_rectangle参数只有正式API Key 才能使用 ，试用API key 无法使用\n",
    "\n",
    "# 5.payload：酬载，必要阅读api文档中的*必选项*和*可选性*，及*参数说明*\n",
    "payload = {\n",
    "    'api_key':'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_',\n",
    "    'api_secret':'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9',       \n",
    "}\n",
    "\n",
    "r = requests.post(base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "51de545a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:13:40.341475Z",
     "start_time": "2022-03-21T13:13:40.331474Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f6c3fbce",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:14:10.875221Z",
     "start_time": "2022-03-21T13:14:10.866221Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '581804406d1a7e2b753d486177e0b207',\n",
       " 'time_used': 155,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647868419,d2ee24b2-26e3-4494-ad21-11c4ecc02a10',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35ac5035",
   "metadata": {},
   "source": [
    "### 8. 添加人脸api"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "fa7b211d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:25:13.867142Z",
     "start_time": "2022-03-21T13:25:12.937089Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key = 'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_'\n",
    "API_sercret = 'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9'\n",
    "\n",
    "# 3.POST\n",
    "\n",
    "\n",
    "# 4.所有API key 都可以调用本API\n",
    "#    其中calculate_all 和 face_rectangle参数只有正式API Key 才能使用 ，试用API key 无法使用\n",
    "\n",
    "# 5.payload：酬载，必要阅读api文档中的*必选项*和*可选性*，及*参数说明*\n",
    "payload = {\n",
    "    'api_key':'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_',\n",
    "    'api_secret':'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9',\n",
    "    'faceset_token':'581804406d1a7e2b753d486177e0b207',\n",
    "    'face_tokens': '13b646dfdc68464f90470d2dc5001075'\n",
    "}\n",
    "\n",
    "r = requests.post(base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "e69af081",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:25:15.379229Z",
     "start_time": "2022-03-21T13:25:15.359228Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "68419348",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:25:19.342456Z",
     "start_time": "2022-03-21T13:25:19.332455Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '581804406d1a7e2b753d486177e0b207',\n",
       " 'time_used': 593,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647869114,8652bbfd-d24c-4267-95fa-67c4533938ed',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c88b5f40",
   "metadata": {},
   "source": [
    "###  9. 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "1ecc726c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:25:34.530324Z",
     "start_time": "2022-03-21T13:25:33.651274Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key = 'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_'\n",
    "API_sercret = 'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9'\n",
    "\n",
    "# 3.POST\n",
    "\n",
    "\n",
    "# 4.所有API key 都可以调用本API\n",
    "#    其中calculate_all 和 face_rectangle参数只有正式API Key 才能使用 ，试用API key 无法使用\n",
    "\n",
    "# 5.payload：酬载，必要阅读api文档中的*必选项*和*可选性*，及*参数说明*\n",
    "payload = {\n",
    "    'api_key':'UBn-zQCWC0WN7G6ONfvTFEkymVWPqqm_',\n",
    "    'api_secret':'iYfVaxJLVXyqeTNys-O_d1Vz-Ij_5P-9',       \n",
    "    'face_token': '13b646dfdc68464f90470d2dc5001075',\n",
    "    'faceset_token': '581804406d1a7e2b753d486177e0b207'\n",
    "}\n",
    "r = requests.post(base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c25d7fb2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:25:36.577441Z",
     "start_time": "2022-03-21T13:25:36.567441Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "4e08fefb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T13:25:39.023581Z",
     "start_time": "2022-03-21T13:25:39.010581Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647869134,ba1399d1-54cc-4059-978a-3bcd90d8e629',\n",
       " 'time_used': 611,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.389,\n",
       "   'user_id': '',\n",
       "   'face_token': '13b646dfdc68464f90470d2dc5001075'}]}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f31044d8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb260dee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8af8126b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "position": {
    "height": "144.85px",
    "left": "1145px",
    "right": "20px",
    "top": "115px",
    "width": "350px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
